package com.zyh.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;


@WebServlet("/cookie")
public class CookieStudyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        Cookie cookieA = new Cookie("name", "cyrus");
        Cookie cookieB = new Cookie("msg", URLEncoder.encode("会话技术"));
        cookieB.setMaxAge(30);
        resp.addCookie(cookieA);
        resp.addCookie(cookieB);

        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("<h1>Cookie学习</h1>");
    }
}
